package com.thunder.rebot.rebotclient.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.widget.ImageView;

//import com.github.chrisbanes.photoview.OnMatrixChangedListener;
//import com.github.chrisbanes.photoview.OnOutsidePhotoTapListener;
//import com.github.chrisbanes.photoview.OnPhotoTapListener;
//import com.github.chrisbanes.photoview.OnScaleChangedListener;
//import com.github.chrisbanes.photoview.OnSingleFlingListener;
//import com.github.chrisbanes.photoview.PhotoView;
//import com.github.chrisbanes.photoview.PhotoViewAttacher;
import com.thunder.rebot.rebotclient.iml.OnCustomMatrixChangedListener;
import com.thunder.rebot.rebotclient.iml.OnCustomOutsidePhotoTapListener;
import com.thunder.rebot.rebotclient.iml.OnCustomPhotoTapListener;
import com.thunder.rebot.rebotclient.iml.OnCustomScaleChangedListener;
import com.thunder.rebot.rebotclient.iml.OnCustomSingleFlingListener;
import com.thunder.rebot.rebotclient.iml.OnDragListener;
import com.thunder.rebot.rebotclient.iml.OnMyFling;
import com.thunder.rebot.rebotclient.iml.OnScrollListener;

public class MyPhontoView extends ImageView {


    private String TAG = MyPhontoView.class.getSimpleName();


    private MyPhotoViewAttacher myAttacher;

//    private PhotoViewAttacher myAttacher;

    public MyPhontoView(Context context) {
        this(context, null);
    }

    public MyPhontoView(Context context, AttributeSet attr) {
        this(context, attr, 0);
    }

    public MyPhontoView(Context context, AttributeSet attr, int defStyle) {
        super(context, attr, defStyle);
        init();
    }

    @TargetApi(21)
    public MyPhontoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        myAttacher = new MyPhotoViewAttacher(this);
        //We always pose as a Matrix scale type, though we can change to another scale type
        //via the myAttacher
        super.setScaleType(ScaleType.MATRIX);
    }

    /**
     * Get the current {@link PhotoViewAttacher} for this view. Be wary of holding on to references
     * to this myAttacher, as it has a reference to this view, which, if a reference is held in the
     * wrong place, can cause memory leaks.
     *
     * @return the myAttacher.
     */
    public MyPhotoViewAttacher getAttacher() {
        return myAttacher;
    }

    @Override
    public ScaleType getScaleType() {
        return myAttacher.getScaleType();
    }

    @Override
    public Matrix getImageMatrix() {
        myAttacher.update();
        return myAttacher.getImageMatrix();
    }

    @Override
    public void setOnLongClickListener(OnLongClickListener l) {
        myAttacher.setOnLongClickListener(l);
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        myAttacher.setOnClickListener(l);
    }



    @Override
    public void setScaleType(ScaleType scaleType) {
        if (myAttacher != null) {
            myAttacher.setScaleType(scaleType);
        }
    }

    @Override
    public void setImageDrawable(Drawable drawable) {
        super.setImageDrawable(drawable);
        // setImageBitmap calls through to this method
        if (myAttacher != null) {
            myAttacher.update();
        }
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        if (myAttacher != null) {
            myAttacher.update();
        }
    }

    @Override
    public void setImageURI(Uri uri) {
        super.setImageURI(uri);
        if (myAttacher != null) {
            myAttacher.update();
        }
    }

    @Override
    protected boolean setFrame(int l, int t, int r, int b) {
        boolean changed = super.setFrame(l, t, r, b);
        if (changed && myAttacher != null) {
            myAttacher.update();
        }
        return changed;
    }

    public void setRotationTo(float rotationDegree) {
        myAttacher.setRotationTo(rotationDegree);
    }

    public void setRotationBy(float rotationDegree) {
        myAttacher.setRotationBy(rotationDegree);
    }

    public boolean isZoomEnabled() {
        return myAttacher.isZoomEnabled();
    }

    public RectF getDisplayRect() {
        return myAttacher.getDisplayRect();
    }

    public void getDisplayMatrix(Matrix matrix) {
        myAttacher.getDisplayMatrix(matrix);
    }

    public boolean setDisplayMatrix(Matrix finalRectangle) {
        return myAttacher.setDisplayMatrix(finalRectangle);
    }

    public float getMinimumScale() {
        return myAttacher.getMinimumScale();
    }

    public float getMediumScale() {
        return myAttacher.getMediumScale();
    }

    public float getMaximumScale() {
        return myAttacher.getMaximumScale();
    }

    public float getScale() {
        return myAttacher.getScale();
    }

    public void setAllowParentInterceptOnEdge(boolean allow) {
        myAttacher.setAllowParentInterceptOnEdge(allow);
    }

    public void setMinimumScale(float minimumScale) {
        myAttacher.setMinimumScale(minimumScale);
    }

    public void setMediumScale(float mediumScale) {
        myAttacher.setMediumScale(mediumScale);
    }

    public void setMaximumScale(float maximumScale) {
        myAttacher.setMaximumScale(maximumScale);
    }

    public void setScaleLevels(float minimumScale, float mediumScale, float maximumScale) {
        myAttacher.setScaleLevels(minimumScale, mediumScale, maximumScale);
    }

    public void setOnMatrixChangeListener(OnCustomMatrixChangedListener listener) {
        myAttacher.setOnMatrixChangeListener(listener);
    }

    public void setOnPhotoTapListener(OnCustomPhotoTapListener listener) {
        myAttacher.setOnPhotoTapListener(listener);
    }

    public void setOnOutsidePhotoTapListener(OnCustomOutsidePhotoTapListener listener) {
        myAttacher.setOnOutsidePhotoTapListener(listener);
    }

    public void setScale(float scale) {
        myAttacher.setScale(scale);
    }

    public void setScale(float scale, boolean animate) {
        myAttacher.setScale(scale, animate);
    }

    public void setScale(float scale, float focalX, float focalY, boolean animate) {
        myAttacher.setScale(scale, focalX, focalY, animate);
    }

    public void setZoomable(boolean zoomable) {
        myAttacher.setZoomable(zoomable);
    }

    public void setZoomTransitionDuration(int milliseconds) {
        myAttacher.setZoomTransitionDuration(milliseconds);
    }

    public void setOnDoubleTapListener(GestureDetector.OnDoubleTapListener newOnDoubleTapListener) {
        myAttacher.setOnDoubleTapListener(newOnDoubleTapListener);
    }

    public void setOnScaleChangeListener(OnCustomScaleChangedListener onScaleChangedListener) {
        myAttacher.setCustomOnScaleChangeListener(onScaleChangedListener);
    }

    public void setOnSingleFlingListener(OnCustomSingleFlingListener onSingleFlingListener) {
        myAttacher.setCustomOnSingleFlingListener(onSingleFlingListener);
    }

    public void setOnScrollListener(OnScrollListener mOnScrollListener){
        myAttacher.setOnScrollListener(mOnScrollListener);
    }
//    public void setOnDragListener(OnDragListener onDragListener){
//        myAttacher.setOnDragListener(onDragListener);
//    }
    public void setOnFlingListener(OnMyFling onFlingListener){
        myAttacher.setOnFlingListener(onFlingListener);
    }

    public void setOnDragListener(com.thunder.rebot.rebotclient.iml.OnDragListener onDragListener) {
        myAttacher.setOnDragListener(onDragListener);
    }
}
